package pt.isel.lercm.projectmanager.spring.model;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name="ORIENTADOR")
public class Orientador {

	private int id;
	private String nome;
	private Set<Grupo> grupos;
	private String userGithub;
	private String userRedmine;
	private User user;
	private String email;
	
	@Id
	@SequenceGenerator(name="ID_GENERATOR", sequenceName="hibernate_sequence", allocationSize=1)  
	@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ID_GENERATOR")  
	@Column(name="ID", unique = true, nullable = false)
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	@Column(name="NOME", nullable = true)
	public String getNome() {
		return nome;
	}
	public void setNome(String nome) {
		this.nome = nome;
	}
	
	@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	@JoinTable(name = "orientador_grupo", joinColumns = { 
			@JoinColumn(name = "ORIENTADOR_ID", nullable = false, updatable = false) }, 
			inverseJoinColumns = { @JoinColumn(name = "GRUPO_ID", 
					nullable = false, updatable = false) })
	public Set<Grupo> getGrupos() {
		return grupos;
	}
	public void setGrupos(Set<Grupo> grupos) {
		this.grupos = grupos;
	}
	
	@Column(name="USER_GITHUB", nullable = true)
	public String getUserGithub() {
		return userGithub;
	}
	public void setUserGithub(String userGithub) {
		this.userGithub = userGithub;
	}
	
	@Column(name="USER_REDMINE", nullable = true)
	public String getUserRedmine() {
		return userRedmine;
	}
	public void setUserRedmine(String userRedmine) {
		this.userRedmine = userRedmine;
	}
	
	@OneToOne
    @JoinColumn(name = "user_id")
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	
	@Column(name="EMAIL", nullable = true)
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	
	@Transient
	public boolean isNew(){
		return id == 0;
	}
	
	@Transient
	public boolean equals(Object obj){
		if (obj == null)
            return false;
        if (obj == this)
            return true;
        if (!(obj instanceof Orientador))
            return false;

        Orientador rhs = (Orientador) obj;
        
        return rhs.getId() == this.id;
	}
	
}
